package util.preprocessing;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;

import util.delicious.DmozEntryDescription;
import util.hashing.Sorting;
import util.io.FileInput;

public class Borreme {

	public static long window=(long) 1800;	
	
	public static void orderAOL() {

		String aol = "/project/data/AOL_queryLOG/allAOL2.txt";
		FileInput in = new FileInput(aol);

		String tmp = in.readString();
		while (tmp.startsWith("AnonID"))
			tmp = in.readString().trim();

		Hashtable<String, Float> hash = new Hashtable<String, Float>();

		long index = 0;
		String user = "";
		String newuser = "";
		String old[] = tmp.trim().split("\t");
		Timestamp init = java.sql.Timestamp.valueOf(old[2].trim());

		user = old[0].trim();
		hash.put(tmp + "&&&&" + index, (float) init.getTime()/1000);
		index++;
		String line;
		
		
		while (!in.eof()) {

			String curr[] = null;

			line = in.readString();
			while (line != null && line.startsWith("AnonID"))
				line = in.readString();

			if (line != null) {
				curr = line.trim().split("\t");
				
				newuser = curr[0].trim();
				init = java.sql.Timestamp.valueOf(curr[2].trim());
			}

			while (line != null && newuser.equals(user)) {

				hash.put(line + "&&&&" + index, (float) init.getTime()/1000);
				user = newuser;

				line = in.readString();

				while (line!=null && line.startsWith("AnonID"))
					line = in.readString().trim();

				if (line != null) {
					curr = line.trim().split("\t");
				
					init = java.sql.Timestamp.valueOf(curr[2].trim());
					newuser = curr[0];
				}
				index++;
			}

			
			
			Iterator<Entry<String, Float>> items = Sorting
					.sortHashNumericValues(hash, true);

			

			while (items.hasNext()) {

				Entry<String, Float> item = items.next();
				String li = item.getKey();
				String tt[] = li.split("&&&&");
				System.out.println(tt[0]);

			}

			hash.clear();
			user = newuser;
			hash = new Hashtable<String, Float>();
			hash.put(line + "&&&&" + index, (float) init.getTime()/1000);
			index++;

		}

		Iterator<Entry<String, Float>> items = Sorting.sortHashNumericValues(
				hash, true);
		while (items.hasNext()) {

			Entry<String, Float> item = items.next();
			String li = item.getKey();
			String tt[] = li.split("&&&&");
			System.out.println(tt[0]);

		}

	}
	
	
	public boolean isAfter(Timestamp init, Timestamp b){
		
		
		
		if(init.after(b)){
			
			return true;
		}else{
			
			
		}
		
	return false;	
	}
	
	
	public static void getSessionTime(String path) {
		FileInput in = new FileInput(path);

		String tmp = in.readString();
		String previous[] = tmp.split("\t");

		int SESSION_POS = 0;
		int TIME_POS = 3;
		while (tmp != null) {

			Timestamp a = null;
			Timestamp b = null;
			Timestamp min = null;
			Timestamp max =null; 
			
			if(previous!=null && previous.length>TIME_POS){
			 min = java.sql.Timestamp.valueOf(previous[TIME_POS]);
			 max = java.sql.Timestamp.valueOf(previous[TIME_POS]);
			
			}

			String bridge = "";
			String tmp2 = in.readString();

			while (tmp2 != null && !tmp2.equals("\\s")	&& tmp2.split("\t")[SESSION_POS].trim().equals(
							previous[SESSION_POS])) {
				bridge = tmp2;

				
				if(bridge.split("\t").length>=3){
				b = java.sql.Timestamp.valueOf(bridge.split("\t")[TIME_POS]	.trim());
			
				
				if (b.getTime() > max.getTime()) {

					max = b;
				}

				if (b.getTime() < min.getTime()) {
					min = b;

				}
				
				
				
				
				}
				//System.out.println("Session:" +tmp2.split("\t")[0]+ "Max:"  + max.getTime() + " Min" + min.getTime() );
				tmp2 = in.readString();

			}

			
			if (bridge!=null && !bridge.equals("") && max!=null && min!=null) {

				double time = max.getTime() / 1000 - min.getTime() / 1000;
				time = Math.abs(time/60);
				if (time < 0) {

					System.out.println("Previous: " + previous[TIME_POS] + "  "
							+ bridge);
				}

				System.out.println(time);

			}
			
			
			
			
			
			if (tmp2 != null)
				previous = tmp2.split("\t");
			tmp = tmp2;

		}

	}
	
	public static void getGoalsTime(String path) {
		FileInput in = new FileInput(path);

		String tmp = in.readString();
		String previous[] = tmp.split("\t");

		int SESSION_POS = 0;
		
		
		
		String current_session="";
		String previous_session=tmp.split("\t")[SESSION_POS].trim() + "\t"+tmp.split("\t")[SESSION_POS+1].trim() ;
		
		
		int TIME_POS = 4;
		while (tmp != null) {

			Timestamp a = null;
			Timestamp b = null;
			Timestamp min = null;
			Timestamp max =null; 
			
			if(previous!=null && previous.length>TIME_POS){
			 min = java.sql.Timestamp.valueOf(previous[TIME_POS]);
			 max = java.sql.Timestamp.valueOf(previous[TIME_POS]);
			
			}

			String bridge = "";
			String tmp2 = in.readString();

			if(tmp2!=null && tmp2.split("\t").length>SESSION_POS+1)
			current_session = tmp2.split("\t")[SESSION_POS].trim() + "\t"+tmp2.split("\t")[SESSION_POS+1].trim()  ;
			
			while (tmp2 != null && !tmp2.equals("\\s")	&& current_session.equals(previous_session)) {
				bridge = tmp2;

				
				if(bridge.split("\t").length>=3){
				b = java.sql.Timestamp.valueOf(bridge.split("\t")[TIME_POS]	.trim());
			
				
				if (b.getTime() > max.getTime()) {

					max = b;
				}

				if (b.getTime() < min.getTime()) {
					min = b;

				}
				
				
				
				
				}
				//System.out.println("Session:" +tmp2.split("\t")[0]+ "Max:"  + max.getTime() + " Min" + min.getTime() );
				tmp2 = in.readString();
				
				if(tmp2!=null)
				current_session = tmp2.split("\t")[SESSION_POS].trim() + "\t"+tmp2.split("\t")[SESSION_POS+1].trim()  ;

			}

			
			if (!bridge.equals("")) {

				double time = max.getTime() / 1000 - min.getTime() / 1000;
				time = Math.abs(time/60);
				if (time < 0) {

					System.out.println("Previous: " + previous[TIME_POS] + "  "
							+ bridge);
				}

				System.out.println(time);

			}
			if (tmp2 != null){
				previous = tmp2.split("\t");
				previous_session  = current_session;
			
			}
			tmp = tmp2;

		}

	}
	
	public static void getTimeConsecutive(String path) {
		FileInput in = new FileInput(path);

		String tmp = in.readString();
		String previous[] = tmp.split("\t");

		int SESSION_POS = 0;
		int TIME_POS = 3;
		while (tmp != null) {

			String bridge = "";
			String tmp2 = in.readString();
			while (tmp2 != null
					&& tmp2.split("\t").length >= 4
					&& tmp2.split("\t")[SESSION_POS].trim().equals(
							previous[SESSION_POS])) {
				bridge = tmp2;
				tmp2 = in.readString();
				double time = getTimeDifference(previous[TIME_POS], bridge
						.split("\t")[TIME_POS].trim());
				time = Math.abs(time);

				System.out.println(time);

			}

			if (!bridge.equals("")) {

				double time = getTimeDifference(previous[TIME_POS], bridge
						.split("\t")[TIME_POS].trim());
				time = Math.abs(time);
				if (time < 0) {

					System.out.println("Previous: " + previous[TIME_POS] + "  "
							+ bridge);
				}

				System.out.println(time);

			}

			if (tmp2 != null)
				previous = tmp2.split("\t");
			tmp = tmp2;
		}
	}
	
	private static double getTimeDifference(String string, String trim) {
		// TODO Auto-generated method stub

		Timestamp a = java.sql.Timestamp.valueOf(string.trim());
		Timestamp b = java.sql.Timestamp.valueOf(trim.trim());

		double t = b.getTime() / 1000 - a.getTime() / 1000;

		return t;
	}

	public static void extractSession2(boolean onlyKids) {

		String aol = "/project/data/AOL_queryLOG/allAOL_sorted.txt";
		String path = "/project/data/dmoz/kt/kt-content.rdf.u8";
		FileInput in = new FileInput(aol);

		String tmp = in.readString();

		StringBuffer session = new StringBuffer("");

		String old_user = "";
		String newuser = "";
		String old[] = tmp.trim().split("\t");
	
		Timestamp init = java.sql.Timestamp.valueOf(old[2].trim());
		Timestamp end = null;
		old_user = old[0].trim();

		long sessionId = 0;
		String line = tmp;
		
		
		Hashtable<String, Integer[]> hash = null;

		if (onlyKids) {
			hash = dmozExtractor.genKT_URLandType(path);
		}
		
		boolean startsWithKids = false;
		boolean containsKids = false;
		
		while (!in.eof()) {

			String curr[] = null;

			session = new StringBuffer(sessionId + "\t" + line);
			
			 startsWithKids = false;
			 containsKids = false;
			
			if (hash != null
					&& evaluateDomainInDmoz(line.trim().split("\t"), hash, true, false,
							false)) {

				startsWithKids = true;

			}
			
			
			line = in.readString();

			if (line != null) {
				curr = line.trim().split("\t");
				
				newuser = curr[0].trim();
				end = java.sql.Timestamp.valueOf(curr[2].trim());
			}

			while (line != null && newuser.equals(old_user) &&

			( (end.getTime()/1000) - (init.getTime()/1000)) <= window) {

				old_user = newuser;
				init = end;
				session.append("\n" + sessionId + "\t" + line.trim());
				
				if (hash != null
						&& evaluateDomainInDmoz(line.trim().split("\t"), hash, true, false,
								false)) {

					containsKids = true;

				}
				
				
				line = in.readString();

				if (line != null && !line.equals("null") && !line.trim().equals("")) {
					
					curr = line.trim().split("\t");
					
					//System.out.println(line);
					end = java.sql.Timestamp.valueOf(curr[2].trim());
					newuser = curr[0];
				}

			}

			
			if(onlyKids){
				if(startsWithKids)
				System.out.println(session);	
				
			}else{
				System.out.println(session);
				
			}
			
			init =end;
			old_user=newuser;
			sessionId++;			
		}
		
		
		if(onlyKids){
			if(startsWithKids)
			System.out.println(session);	
			
		}else{
			System.out.println(session);
			
		}

	}
	
	
	/**
	 * 
	 * Extract sessions from the AOL query Log
	 * 
	 *  
	 * 
	 * @param mode 0 for all sessions, 1 for sessions starting with Kids and teens seeds, 2 for sessions containing kids and teens seeds, 3 sessions ending in 
	 * Kids and teens seed.
	 * 
	 * @param window: Allowed logout time between queries specified in seconds 
	 * 
	 * 
	 */

	public static void extractSession3(int mode, long window, boolean kids, boolean teens, boolean mteens) {

		String aol = "/home/sergio/projects/data/AOL_queryLOG/allAOL.txt";
		String path = "/home/sergio/projects/data/dmoz/kt/kt-content.rdf.u8";
		
		FileInput in = new FileInput(aol);

		String tmp = in.readString();

		StringBuffer session = new StringBuffer("");

		String old_user = "";
		String newuser = "";
		String old[] = tmp.trim().split("\t");

		Timestamp init = java.sql.Timestamp.valueOf(old[2].trim());
		Timestamp end = null;
		old_user = old[0].trim();

		long sessionId = 0;
		String line = tmp;

		Hashtable<String, Integer[]> hash = null;

		if (mode != 0) {
			hash = dmozExtractor.genKT_URLandType(path);
		}

		boolean startsWithKids = false;
		boolean containsKids = false;
		boolean endsWithKids = false;

		while (!in.eof()) {

			String curr[] = null;

			session = new StringBuffer(sessionId + "\t" + line);

			startsWithKids = false;
			containsKids = false;

			if (hash != null
					&& evaluateDomainInDmoz(line.trim().split("\t"), hash,
							kids, teens, mteens)) {

				startsWithKids = true;
				containsKids = true;

			}

			line = in.readString();
			
			if(line.startsWith("AnonID")){
				line = in.readString();
				
			}

			if (line != null) {
				curr = line.trim().split("\t");

				newuser = curr[0].trim();
				end = java.sql.Timestamp.valueOf(curr[2].trim());
			}

			while (line != null && newuser.equals(old_user) &&

			((end.getTime() / 1000) - (init.getTime() / 1000)) <= window) {

				old_user = newuser;
				init = end;
				endsWithKids = false;
				session.append("\n" + sessionId + "\t" + line.trim());

				if (hash != null
						&& evaluateDomainInDmoz(line.trim().split("\t"), hash,
								kids, teens, mteens)) {

					endsWithKids = true;

				}

				if (hash != null
						&& evaluateDomainInDmoz(line.trim().split("\t"), hash,
								kids, teens, mteens)) {

					containsKids = true;

				}

				line = in.readString();
							
				if(line.startsWith("AnonID")){
					line = in.readString();
					
				}
				
				

				if (line != null && !line.equals("null")
						&& !line.trim().equals("")) {

					curr = line.trim().split("\t");

					// System.out.println(line);
					end = java.sql.Timestamp.valueOf(curr[2].trim());
					newuser = curr[0];
				}

			}

			if (mode == 1 && startsWithKids) {

				System.out.println(session);

			} else if (mode == 2 && containsKids) {
				System.out.println(session);

			} else if (mode == 3 && endsWithKids) {

				System.out.println(session);
			} else if (mode == 0) {

				System.out.println(session);
			}

			init = end;
			old_user = newuser;
			sessionId++;
		}

	
	}
	
	
	
	/**
	 * 
	 * Extract sessions from the AOL query Log
	 * 
	 *  
	 * 
	 * @param mode 0 for all sessions, 1 for sessions starting with Kids and teens seeds, 2 for sessions containing kids and teens seeds, 3 sessions ending in 
	 * Kids and teens seed.
	 * 
	 * @param window: Allowed logout time between queries specified in seconds 
	 * 
	 * 
	 */

	public static void extractSessionDmoz(long window, String filter) {

		String aol = "/home/sergio/projects/data/AOL_queryLOG/allAOL_sortedCleaned.txt";
		String path = "/home/sergio/projects/data/dmoz/content.rdf.u8";

		FileInput in = new FileInput(aol);

		String tmp = in.readString();

		StringBuffer session = new StringBuffer("");

		String old_user = "";
		String newuser = "";
		String old[] = tmp.trim().split("\t");

		Timestamp init = java.sql.Timestamp.valueOf(old[2].trim());
		Timestamp end = null;
		old_user = old[0].trim();

		long sessionId = 0;
		String line = tmp;

		Hashtable<String, DmozEntryDescription> hash = null;
	
		
			hash = dmozExtractor.getURLandInfo(path, filter);
		
			
		boolean startSession = true;
		boolean containSession = true;

		while (!in.eof()) {

			String curr[] = null;

			session = new StringBuffer(sessionId + "\t" + line);
			containSession = false;
			
			
			if (hash != null && line.trim().split("\t").length > 4
					&& hash.containsKey(line.trim().split("\t")[4].trim())) 
				{
					containSession = true;

				}

				line = in.readString();
				if(line.startsWith("AnonID")){
					line = in.readString();
					
				}
				
				
			
				if (line != null) {
					curr = line.trim().split("\t");

					newuser = curr[0].trim();
					end = java.sql.Timestamp.valueOf(curr[2].trim());
				}

				while (line != null && newuser.equals(old_user) &&
						
				((end.getTime() / 1000) - (init.getTime() / 1000)) <= window) {

					old_user = newuser;
					init = end;
					session.append("\n" + sessionId + "\t" + line.trim());

					if (hash != null && line.trim().split("\t").length > 4 
							&& hash.containsKey(line.trim().split("\t")[4]
									.trim())) {

						containSession = true;
					}

					line = in.readString();
					
					if(line!=null && line.startsWith("AnonID")){
						line = in.readString();
						
					}
					
					if (line != null && !line.equals("null")
							&& !line.trim().equals("")) {

						curr = line.trim().split("\t");

						// System.out.println(line);
						end = java.sql.Timestamp.valueOf(curr[2].trim());
						newuser = curr[0];
					}

				}

				if (containSession) {

					System.out.println(session);
				}

				init = end;
				old_user = newuser;
				sessionId++;
			}
		

	}

	private static boolean evaluateDomainInDmoz(String host[],
			Hashtable<String, Integer[]> hash, boolean kids, boolean teens,
			boolean mteens) {

		if (host.length > 4) {

			if (hash.containsKey(host[4].trim())) {

				Integer i[] = hash.get(host[4].trim());

				if (aolQueryLogExtractor.satisfyMode(i, kids, teens, mteens)) {
					return true;
				}
			}
		}
		return false;

	}
	
	
	public static  void printLengthGoals(String path) {

		FileInput in = new FileInput(path);

		String tmp = in.readString();
		String previous[] = tmp.split("\t");

		int SESSION_POS = 0;

		String current_session = "";
		String previous_session = tmp.split("\t")[SESSION_POS].trim() + "\t"
				+ tmp.split("\t")[SESSION_POS + 1].trim();

		int TIME_POS = 4;
		while (tmp != null) {
			
			int length=1;

			Timestamp a = null;
			Timestamp b = null;
			Timestamp min = null;
			Timestamp max = null;

			if (previous != null && previous.length > TIME_POS) {
				min = java.sql.Timestamp.valueOf(previous[TIME_POS]);
				max = java.sql.Timestamp.valueOf(previous[TIME_POS]);

			}

			String bridge = "";
			String tmp2 = in.readString();

			if (tmp2 != null && tmp2.split("\t").length > SESSION_POS + 1)
				current_session = tmp2.split("\t")[SESSION_POS].trim() + "\t"
						+ tmp2.split("\t")[SESSION_POS + 1].trim();
			
			while (tmp2 != null && !tmp2.equals("\\s")
					&& current_session.equals(previous_session)) {
				bridge = tmp2;
				length++;
				
				
				if (bridge.split("\t").length >= 3) {
					b = java.sql.Timestamp.valueOf(bridge.split("\t")[TIME_POS]
							.trim());

					if (b.getTime() > max.getTime()) {

						max = b;
					}

					if (b.getTime() < min.getTime()) {
						min = b;

					}

				}
				// System.out.println("Session:" +tmp2.split("\t")[0]+ "Max:" +
				// max.getTime() + " Min" + min.getTime() );
				tmp2 = in.readString();

				if (tmp2 != null)
					current_session = tmp2.split("\t")[SESSION_POS].trim()
							+ "\t" + tmp2.split("\t")[SESSION_POS + 1].trim();
				

			}

			if (!bridge.equals("")) {

				double time = max.getTime() / 1000 - min.getTime() / 1000;
				time = Math.abs(time / 60);
				if (time < 0) {

					System.out.println("Previous: " + previous[TIME_POS] + "  "
							+ bridge);
				}

				System.out.println(length);

			}else{
				
				System.out.println(length);
				
			}
			if (tmp2 != null) {
				previous = tmp2.split("\t");
				previous_session = current_session;

			}
			tmp = tmp2;

		}

	}
	
	public static void main(String args[]) {

		//orderAOL();
		String path ="/home/sergio/projects/data/AOL_queryLOG/goals_All.txt";
		
	//	extractSessionDmoz(window,"Top/Business/Financial_Services");
		
		//printLengthGoals(path);
		path = "/home/sergio/projects/data/AOL_queryLOG/financial_sessions.txt";
		
		getTimeConsecutive(path);
		
		
	//	Timestamp init = java.sql.Timestamp.valueOf("2006-05-31 21:49:45");
	//	Timestamp end=null;
	//	System.out.println((float)init.getTime());
		
	//	init = java.sql.Timestamp.valueOf("2006-05-31 21:49:56");
		
		
		
	}

}
